﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace AEM.Controls
{
    /// <summary>
    /// OuterRingModel.xaml 的交互逻辑
    /// </summary>
    public partial class OuterRingModel : UserControl
    {
        public OuterRingModel()
        {
            InitializeComponent();
        }

        private static void OnFloorHeightChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                OuterRingModel o = sender as OuterRingModel;
                IEnumerable<OuterFloor> ie = o.floorInfo.Children.OfType<OuterFloor>();
                int oldheight = (int)args.OldValue;
                Array.ForEach<OuterFloor>(ie.ToArray<OuterFloor>(), new Action<OuterFloor>((v) =>
                {
                    o.Height = o.MaxHeight = o.MinHeight += o.FloorHeight - 1 - oldheight;
                    v.Height = o.FloorHeight;
                }));
            }
        }

        private static void OnMaxFloorsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue && ((int)args.NewValue > (int)args.OldValue))
            {
                OuterRingModel o = sender as OuterRingModel;
                IEnumerable<OuterFloor> ie = o.floorInfo.Children.OfType<OuterFloor>();
                Array.ForEach<OuterFloor>(ie.ToArray<OuterFloor>(), new Action<OuterFloor>((v) =>
                {
                    o.UnregisterName(v.Name);
                    o.floorInfo.Children.Remove(v);
                }));
                int floors = (int)args.NewValue;
                for (int i = floors; i > 0; i--)
                {
                    OuterFloor of = new OuterFloor();
                    o.RegisterName("f" + i, of);
                    of.Name = "f" + i;
                    of.Height = o.FloorHeight;
                    o.floorInfo.Children.Add(of);
                    o.Height = o.MaxHeight = o.MinHeight += of.Height - 1;
                }
            }
        }

        private static void OnFloorsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue && (((string)args.NewValue).Length > ((string)args.OldValue).Length))
            {
                OuterRingModel o = sender as OuterRingModel;
                IEnumerable<OuterFloor> ie = o.floorInfo.Children.OfType<OuterFloor>();
                Array.ForEach<OuterFloor>(ie.ToArray<OuterFloor>(), new Action<OuterFloor>((v) =>
                {
                    o.UnregisterName(v.Name);
                    o.floorInfo.Children.Remove(v);
                }));
                string[] floors = ((string)args.NewValue).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                int floorheight = AEM.Properties.Settings.Default.FloorHeight;
                if (10 > floorheight) floorheight = 10;
                for (int i = floors.Length; i > 0; i--)
                {
                    OuterFloor of = new OuterFloor();
                    o.RegisterName("f" + i, of);
                    of.Name = "f" + i;
                    of.Height = floorheight;
                    o.floorInfo.Children.Add(of);
                    o.Height = o.MaxHeight = o.MinHeight += of.Height - 1;
                }
            }
        }

        private static void OnUpRingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                IEnumerable<OuterFloor> ie = (sender as OuterRingModel).floorInfo.Children.OfType<OuterFloor>();
                Array.ForEach<OuterFloor>(ie.ToArray<OuterFloor>(), new Action<OuterFloor>((v) =>
                {
                    v.IsUp = Visibility.Hidden;
                }));
                foreach (int i in (List<int>)args.NewValue)
                {
                    OuterFloor rfl = (OuterFloor)(sender as OuterRingModel).floorInfo.FindName("f" + i);
                    if (null != rfl) rfl.IsUp = Visibility.Visible;
                }
            }
        }

        private static void OnDownRingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                IEnumerable<OuterFloor> ie = (sender as OuterRingModel).floorInfo.Children.OfType<OuterFloor>();
                Array.ForEach<OuterFloor>(ie.ToArray<OuterFloor>(), new Action<OuterFloor>((v) =>
                {
                    v.IsDown = Visibility.Hidden;
                }));
                foreach (int i in (List<int>)args.NewValue)
                {
                    OuterFloor of = (OuterFloor)(sender as OuterRingModel).floorInfo.FindName("f" + i);
                    if (null != of) of.IsDown = Visibility.Visible;
                }
            }
        }

        /// <summary>
        /// 楼层
        /// </summary>
        public static readonly DependencyProperty FloorsProperty =
            DependencyProperty.Register("Floors", typeof(string), typeof(OuterRingModel),
            new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnFloorsChanged)));

        /// <summary>
        /// 楼层
        /// </summary>
        public string Floors
        {
            get { return (string)GetValue(FloorsProperty); }
            set { SetValue(FloorsProperty, value); }
        }

        /// <summary>
        /// 外部上招唤的楼层
        /// </summary>
        public static readonly DependencyProperty UpRingProperty =
            DependencyProperty.Register("UpRing", typeof(List<int>), typeof(OuterRingModel),
            new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnUpRingChanged)));

        /// <summary>
        /// 外部上招唤的楼层
        /// </summary>
        public List<int> UpRing
        {
            get { return (List<int>)GetValue(UpRingProperty); }
            set { SetValue(UpRingProperty, value); }
        }

        /// <summary>
        /// 外部上招唤的楼层
        /// </summary>
        public static readonly DependencyProperty DownRingProperty =
            DependencyProperty.Register("DownRing", typeof(List<int>), typeof(OuterRingModel),
            new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnDownRingChanged)));

        /// <summary>
        /// 外部上招唤的楼层
        /// </summary>
        public List<int> DownRing
        {
            get { return (List<int>)GetValue(DownRingProperty); }
            set { SetValue(DownRingProperty, value); }
        }

        public static readonly DependencyProperty FloorHeightProperty =
            DependencyProperty.Register("FloorHeight", typeof(int), typeof(OuterRingModel),
            new FrameworkPropertyMetadata(10, new PropertyChangedCallback(OnFloorHeightChanged)));

        public int FloorHeight
        {
            get { return (int)GetValue(FloorHeightProperty); }
            set { SetValue(FloorHeightProperty, value); }
        }

        public static readonly DependencyProperty MaxFloorsProperty =
            DependencyProperty.Register("MaxFloors", typeof(int), typeof(OuterRingModel),
            new FrameworkPropertyMetadata(2, new PropertyChangedCallback(OnMaxFloorsChanged)));

        public int MaxFloors
        {
            get { return (int)GetValue(MaxFloorsProperty); }
            set { SetValue(MaxFloorsProperty, value); }
        }

    }
}
